$(function(){
	$('#machine-table').bootstrapTable({
		method : 'post',
		url : 'system/machine/getList',
		cache : false,
		contentType: "application/json",//远程数据请求的“contentType”类型（如：application/json，application/x-www-form-urlencoded）。默认值application/json。
		height : $(window).height()-180,
		undefinedText : "--",//定义默认的undefined显示文字。
		striped : true,//使表格带有条纹。
		sortName : "createTime",
		sortOrder : "desc",
		dataType : "json",
		pagination : true,//设置True在表格底部显示分页工具栏。
		sidePagination : "server", //定义表格分页的位置，只能是“client”（设备端）和“server”（服务器端）。
		pageSize : 25,
		pageList : [ 10, 25, 50, 100, 200 ],
		search : true,//启用搜索输入框。
		showColumns : true,//设置为True可显示表格显示/隐藏列表。
		showToggle:true,//设置为True可显示切换普通表格和名片（card）布局。
		showRefresh : true,//设置为True可显示刷新按钮。
		minimumCountColumns : 2,//表格显示/隐藏列表时可设置最小隐藏的列数。
		clickToSelect : true,//设置为True时点击行即可选中单选/复选框。
		toolbar : "#toolbar",//自定义工具栏
		queryParamsType : "limit",//设置为“limit”可以发送标准的RESTFul类型的参数请求。
		queryParams : function(params) {
			return {
				limit : params.limit,
				offset : params.offset,
				sort : params.sort,
				order : params.order,
				or : true,
				machineNum : params.search,
				machineAname : params.search,
				machineCname : params.search,
				machineAddress : params.search
			};
		},
		responseHandler : function(response){
			if(response.result && response.itemCount){
				return {
					"rows": response.result,
					"total": response.itemCount
				};
			}else{
				return {
					"rows": [],
					"total": 0
				};
			}
		},
		columns : [ {
			field : 'state',
			checkbox : true
		}, {
			field : 'machineNum',
			title : '设备编号',
			align : 'center',
			valign : 'middle',
			sortable : true
		}, {
			field : 'machineAname',
			title : '设备别名',
			align : 'center',
			valign : 'middle',
			sortable : true,
			formatter: nameFormatter
		}, {
			field : 'machineOnlineStatus',
			title : '在线状态',
			align : 'center',
			valign : 'middle',
			sortable : true,
			formatter: onlineStatusFormatter
		}, {
			field : 'machineRunStatus',
			title : '开机状态',
			align : 'center',
			valign : 'middle',
			sortable : true,
			formatter : runStatusFormatter
		}, {
			field : 'machineRuningStatus',
			title : '运行状态',
			align : 'center',
			valign : 'middle',
			sortable : true,
			formatter : runingStatusFormatter
		}, {
			field : 'machineAddress',
			title : '设备IP',
			align : 'center',
			valign : 'middle',
            sortable : true
		}, {
			field : 'createTime',
			title : '创建时间',
			align : 'center',
			valign : 'middle',
            sortable : true
		}, {
			field : 'operate',
			title : '操作',
			align : 'center',
			valign : 'middle',
			clickToSelect : false,
			formatter : operateFormatter,
			events : operateEvents
		} ]
	});
	
	$("#addMachine-btn").click(function(){
		$('#machineId').val("");
		$("#machineNum").val("");
		$("#machineNum").removeAttr('readonly');
		$("#machineAname").val("");
		$("#customerId").val('');
		$("#customerId1").val('');
		$("#customerId2").val('');
		$("#customerId3").val('');
		$(".info-label").html("");
		$("#machineinfo-modal").modal("show");
	});
	
	$("#delMachine-btn").click(function(){
		var array = $('#machine-table').bootstrapTable("getSelections");
		if(array!=null && array.length>0){
			var ids = "";
			$.each(array,function(i,v){
				ids+=v.machineId+",";
			});
			bootbox.confirm("您确定要将设备删除吗？", function(result){
				if(result){
					del("system/machine/delMachines/"+ids,function (json, textStatus) {
						if(json.success){
							bootbox.alert("设备删除成功");
							$('#machine-table').bootstrapTable("refresh",{silent: true});
						}else{
							bootbox.alert("设备删除失败");
						}
					});
				}
			});
		}else{
			bootbox.alert("请选择要删除的设备");
		}
	});
	
	$("#machineinfo-save-btn").click(function(){
		var machineId = $('#machineId').val();
		var machineNum = $("#machineNum").val();
		var machineAname = $("#machineAname").val();
		var customerId = $("#customerId").val();
		var customerId1 = $("#customerId1").val();
		var customerId2 = $("#customerId2").val();
		var customerId3 = $("#customerId3").val();
		if(isEmpty(machineNum)){
			$("#machineNum-label").html("<code>*请输入设备手机号*</code>");
			return false;
		}
		if(!isEmpty(customerId)){
			var pattern = /^[0-9]*$/;
			if(!pattern.test(customerId)){
				$("#customerId-label").html("<code>*手机号格式不正确,请输入正确的手机号*</code>");
				return false;
			}
		}
		if(!isEmpty(customerId1)){
			var pattern = /^[0-9]*$/;
			if(!pattern.test(customerId1)){
				$("#customerId-label").html("<code>*手机号格式不正确,请输入正确的手机号*</code>");
				return false;
			}
		}
		if(!isEmpty(customerId2)){
			var pattern = /^[0-9]*$/;
			if(!pattern.test(customerId2)){
				$("#customerId-label").html("<code>*手机号格式不正确,请输入正确的手机号*</code>");
				return false;
			}
		}
		if(!isEmpty(customerId3)){
			var pattern = /^[0-9]*$/;
			if(!pattern.test(customerId3)){
				$("#customerId-label").html("<code>*手机号格式不正确,请输入正确的手机号*</code>");
				return false;
			}
		}
		
		var data = {
				machineNum:machineNum,
				machineAname:machineAname,
				customerId:customerId,
				customerId1:customerId1,
				customerId2:customerId2,
				customerId3:customerId3
		}
		if(isEmpty(machineId)){
			postJson("system/machine/saveMachine",function (json, textStatus){
				if(json.success){
					bootbox.alert("设备添加成功");
					$('#machine-table').bootstrapTable("refresh",{silent: true});
				}else{
					bootbox.alert("设备添加失败:"+json.result);
				}
		    },data);
		}else{
			data['machineId'] = machineId;
			postJson("system/machine/editMachine",function (json, textStatus){
				if(json.success){
					bootbox.alert("设备修改成功");
					$('#machine-table').bootstrapTable("refresh",{silent: true});
				}else{
					bootbox.alert("设备修改失败:"+json.result);
				}
		    },data);
		}
	});
	
	$('#reflush').click(function(){
		var machindId = $('#reflush').data("id");
		if(isEmpty(machindId)){
			bootbox.alert('请重新点击设备详情');
			return false;
		}
		getMachineById(machindId);
	});
	
	$("#switch").click(function(){
		$('#actionPassword-btn').data('action','switch');
		$("#password-modal").modal("show");
	});
	
	$("#actionPassword-btn").click(function(){
		var userId = $('#sessionUserId').text();
		if(isEmpty(userId)){
			$('#actionPassword-label').html('<code>*请刷新页面或者重新登录*</code>');
			return false;
		}
		var password = $('#actionPassword').val();
		if(isEmpty(password)){
			$('#actionPassword-label').html('<code>*请输入密码*</code>');
			return false;
		}
		post("system/machine/checkPassword",function (json, textStatus) {
			if(json!=null && json.success==true){
				$("#password-modal").modal("hide");
				$('#actionPassword').val('');
				$('#actionPassword-label').text('');
				
				var action = $('#actionPassword-btn').data('action');
				if(action==='switch'){//开机或关机
					turnOnMachine();
				}else if(action==='binStall'){//设置料机档位
					setBinStall();
				}else if(action==='adminSet'){//设置有效日期
					setAdminSet();
				}else if(action==='recoverSet'){//恢复出厂设置
					setRecoverSet();
				}else if(action==='timeInterval'){//设置控制时间段
					setTimeInterval();
				}else if(action==='tempTimeout'){//设置温度与延时控制
					setTempTimeout();
				}
			}else{
				bootbox.alert("密码校验 :"+json.result);
			}
		},{userId:userId,password:password});
	});
	
	$('#menu-binStall').click(function(){
		if($('#reflush').data("online")==1)
			$('#binStall-modal').modal('show');
		else
			bootbox.alert('设备离线状态，可以刷新试试');
	});
	$('#binStall-btn').click(function(){
		var machindId = $('#switch').data("id");
		if(isEmpty(machindId)){
			$('#binStall-label').html('<code>*请重新点击设备详情*</code>');
			return false;
		}
		var stall = $('#binStall').val();
		if(isEmpty(stall) || stall==0){
			$('#binStall-label').html('<code>*请选择料机档位*</code>');
			return false;
		}
		$('#actionPassword-btn').data('action','binStall');
		$("#password-modal").modal("show");
	});
	
	$('#menu-tempTimeout').click(function(){
		if($('#reflush').data("online")==1)
			$('#tempTimeout-modal').modal('show');
		else
			bootbox.alert('设备离线状态，可以刷新试试');
	});
	$('#tempTimeout-btn').click(function(){
		var machindId = $('#switch').data("id");
		if(isEmpty(machindId)){
			bootbox.alert('请重新点击设备详情');
			return false;
		}
		var turnOffTemp = $('#turnOffTemp').val();
		if(!isEmpty(turnOffTemp)){
			if(turnOffTemp<1 || turnOffTemp>99){
				bootbox.alert('关火温度取值范围1℃-99℃');
				return false;
			}
		}
		var turnOnTemp = $('#turnOnTemp').val();
		if(!isEmpty(turnOnTemp)){
			if(turnOnTemp<1 || turnOnTemp>99){
				bootbox.alert('开火温度取值范围1℃-99℃');
				return false;
			}
		}
		var smokeTemp = $('#smokeTemp').val();
		if(!isEmpty(smokeTemp)){
			if(smokeTemp<1 || smokeTemp>99){
				bootbox.alert('烟气温度取值范围1℃-99℃');
				return false;
			}
		}
		var turnOnPumpTemp = $('#turnOnPumpTemp').val();
		if(!isEmpty(turnOnPumpTemp)){
			if(turnOnPumpTemp<1 || turnOnPumpTemp>99){
				bootbox.alert('开泵温度取值范围1℃-99℃');
				return false;
			}
		}
		var turnOffPumpTemp = $('#turnOffPumpTemp').val();
		if(!isEmpty(turnOffPumpTemp)){
			if(turnOffPumpTemp<1 || turnOffPumpTemp>99){
				bootbox.alert('停泵温度取值范围1℃-99℃');
				return false;
			}
		}
		var turnOnTimeout = $('#turnOnTimeout').val();
		if(!isEmpty(turnOnTimeout)){
			if(turnOnTimeout<10 || turnOnTimeout>250){
				bootbox.alert('点火器延时10-250秒');
				return false;
			}
		}
		var preFeedTimeout = $('#preFeedTimeout').val();
		if(!isEmpty(preFeedTimeout)){
			if(preFeedTimeout<10 || preFeedTimeout>100){
				bootbox.alert('点火预进料10-100秒');
				return false;
			}
		}
		var turnoffBlowerTimeout = $('#turnoffBlowerTimeout').val();
		if(!isEmpty(turnoffBlowerTimeout)){
			if(turnoffBlowerTimeout<1 || turnoffBlowerTimeout>15){
				bootbox.alert('停鼓风机延时1-15分');
				return false;
			}
		}
		var turnoffWindTimeout = $('#turnoffWindTimeout').val();
		if(!isEmpty(turnoffWindTimeout)){
			if(turnoffWindTimeout<1 || turnoffWindTimeout>30){
				bootbox.alert('停引风机延时1-30分');
				return false;
			}
		}
		$('#actionPassword-btn').data('action','tempTimeout');
		$("#password-modal").modal("show");
	});
	
	var formtime = $('.form_time').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
		autoclose: true,
		startView: 0,
		pickerPosition: 'bottom-left'
    });
	$('#menu-timeInterval').click(function(){
		if($('#reflush').data("online")==1)
			$('#timeInterval-modal').modal('show');
		else
			bootbox.alert('设备离线状态，可以刷新试试');
	});
	$('#timeInterval-btn').click(function(){
		var machindId = $('#switch').data("id");
		if(isEmpty(machindId)){
			bootbox.alert('请重新点击设备详情');
			return false;
		}
		var timeInterval1_1 = $('#workTimeInterval1-1').val();
		var timeInterval1_2 = $('#workTimeInterval1-2').val();
		if(!isEmpty(timeInterval1_1) && !isEmpty(timeInterval1_2)){
			if(timeInterval1_1>=timeInterval1_2){
				bootbox.alert('第一时间段第一个时间必须小于第二个时间');
				return false;
			}
		}
		
		var timeInterval2_1 = $('#workTimeInterval2-1').val();
		var timeInterval2_2 = $('#workTimeInterval2-2').val();
		if(!isEmpty(timeInterval2_1) && !isEmpty(timeInterval2_2)){
			if(timeInterval2_1>=timeInterval2_2){
				bootbox.alert('第二时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval1_1) || isEmpty(timeInterval1_2)){
				bootbox.alert('第二时间段无法保存因为第一时间段为空');
				return false;
			}
			if(timeInterval2_1<=timeInterval1_2){
				bootbox.alert('第二时间段必须大于第一时间段');
				return false;
			}
		}
		
		var timeInterval3_1 = $('#workTimeInterval3-1').val();
		var timeInterval3_2 = $('#workTimeInterval3-2').val();
		if(!isEmpty(timeInterval3_1) && !isEmpty(timeInterval3_2)){
			if(timeInterval3_1>=timeInterval3_2){
				bootbox.alert('第三时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval2_1) || isEmpty(timeInterval2_2)){
				bootbox.alert('第三时间段无法保存因为第二时间段为空');
				return false;
			}
			if(timeInterval3_1<=timeInterval2_2){
				bootbox.alert('第三时间段必须大于第二时间段');
				return false;
			}
		}
		
		var timeInterval4_1 = $('#workTimeInterval4-1').val();
		var timeInterval4_2 = $('#workTimeInterval4-2').val();
		if(!isEmpty(timeInterval4_1) && !isEmpty(timeInterval4_2)){
			if(timeInterval4_1>=timeInterval4_2){
				bootbox.alert('第四时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval3_1) || isEmpty(timeInterval3_2)){
				bootbox.alert('第四时间段无法保存因为第三时间段为空');
				return false;
			}
			if(timeInterval4_1<=timeInterval3_2){
				bootbox.alert('第四时间段必须大于第三时间段');
				return false;
			}
		}
		
		var timeInterval5_1 = $('#workTimeInterval5-1').val();
		var timeInterval5_2 = $('#workTimeInterval5-2').val();
		if(!isEmpty(timeInterval5_1) && !isEmpty(timeInterval5_2)){
			if(timeInterval5_1>=timeInterval5_2){
				bootbox.alert('第五时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval4_1) || isEmpty(timeInterval4_2)){
				bootbox.alert('第五时间段无法保存因为第四时间段为空');
				return false;
			}
			if(timeInterval5_1<=timeInterval4_2){
				bootbox.alert('第五时间段必须大于第四时间段');
				return false;
			}
		}
		
		var timeInterval6_1 = $('#workTimeInterval6-1').val();
		var timeInterval6_2 = $('#workTimeInterval6-2').val();
		if(!isEmpty(timeInterval6_1) && !isEmpty(timeInterval6_2)){
			if(timeInterval6_1>=timeInterval6_2){
				bootbox.alert('第六时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval5_1) || isEmpty(timeInterval5_2)){
				bootbox.alert('第六时间段无法保存因为第五时间段为空');
				return false;
			}
			if(timeInterval6_1<=timeInterval5_2){
				bootbox.alert('第六时间段必须大于第五时间段');
				return false;
			}
		}
		
		var timeInterval7_1 = $('#workTimeInterval7-1').val();
		var timeInterval7_2 = $('#workTimeInterval7-2').val();
		if(!isEmpty(timeInterval7_1) && !isEmpty(timeInterval7_2)){
			if(timeInterval7_1>=timeInterval7_2){
				bootbox.alert('第七时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval6_1) || isEmpty(timeInterval6_2)){
				bootbox.alert('第七时间段无法保存因为第六时间段为空');
				return false;
			}
			if(timeInterval7_1<=timeInterval6_2){
				bootbox.alert('第七时间段必须大于第六时间段');
				return false;
			}
		}
		
		var timeInterval8_1 = $('#workTimeInterval8-1').val();
		var timeInterval8_2 = $('#workTimeInterval8-2').val();
		if(!isEmpty(timeInterval8_1) && !isEmpty(timeInterval8_2)){
			if(timeInterval8_1>=timeInterval8_2){
				bootbox.alert('第八时间段第一个时间必须小于第二个时间');
				return false;
			}
			if(isEmpty(timeInterval7_1) || isEmpty(timeInterval7_2)){
				bootbox.alert('第八时间段无法保存因为第七时间段为空');
				return false;
			}
			if(timeInterval8_1<=timeInterval7_2){
				bootbox.alert('第八时间段必须大于第七时间段');
				return false;
			}
		}
		$('#actionPassword-btn').data('action','timeInterval');
		$("#password-modal").modal("show");
	});
	
	$('#menu-recoverSet').click(function(){
		if($('#reflush').data("online")==1){
			var machindId = $('#switch').data("id");
			if(isEmpty(machindId)){
				$('#recoverSet-label').html('<code>*请重新点击设备详情*</code>');
				return false;
			}
			$('#actionPassword-btn').data('action','recoverSet');
			$("#password-modal").modal("show");
		}else{
			bootbox.alert('设备离线状态，可以刷新试试');
		}
	});
	
	var formdate = $('.form_date').datetimepicker({
        language:  'zh-CN',
        weekStart: 1,
        startDate:new Date(),
        todayBtn:  true,
		autoclose: true,
		todayHighlight: true,
		startView: 2,
		minView: 2,
		forceParse: false,
		pickerPosition: 'bottom-left'
    });
	$('#menu-adminSet').click(function(){
		if($('#reflush').data("online")==1)
			$('#adminSet-modal').modal('show');
		else
			bootbox.alert('设备离线状态，可以刷新试试');
	});
	$('#adminSet-btn').click(function(){
		var machindId = $('#switch').data("id");
		if(isEmpty(machindId)){
			$('#adminSet-label').html('<code>*请重新点击设备详情*</code>');
			return false;
		}
		var adminSetDate = $('#adminSetDate').val();
		if(isEmpty(adminSetDate)){
			$('#adminSet-label').html('<code>*请选择有效日期*</code>');
			return false;
		}
		$('#actionPassword-btn').data('action','adminSet');
		$("#password-modal").modal("show");
	});
});

function turnOnMachine(){
	var id = $('#switch').data('id');
	var value = $('#switch').data('value');
	post("system/machine/turnOnMachine",function (json, textStatus) {
		if(json!=null && json.success==true){
			if(value==true){
				bootbox.alert("设备开机命令已发送");
				$('#mrunstatus').text('开机');
				$('#switch').removeClass('btn-success');
				$('#switch').addClass('btn-danger');
				$('#switch').text('关机');
				$('#switch').data('value',false);
			}else{
				bootbox.alert("设备关机命令已发送");
				$('#mrunstatus').text('关机');
				$('#switch').addClass('btn-success');
				$('#switch').removeClass('btn-danger');
				$('#switch').text('开机');
				$('#switch').data('value',true);
			}
			setTimeout('getMachineById("'+id+'")',3000);
		}else{
			if(value==true)
				bootbox.alert("设备开机失败:"+json.result);
			else
				bootbox.alert("设备关机失败:"+json.result);
		}
	},{machineId:id,turnOn:value});
}

function setBinStall(){
	var id = $('#switch').data("id");
	var value = $('#binStall').val();console.log('档位：'+value);
	post("system/machine/editMachineBinStall",function (json, textStatus) {
		if(json!=null && json.success==true){
			$('#binStall-modal').modal('hide');
			$('#mbinstall').text(value);
			bootbox.alert("设置料机档位修改命令已发送");
		}else{
			bootbox.alert("设置料机档位失败:"+json.result);
		}
	},{machineId:id,binStall:value});
}

function setAdminSet(){
	var id = $('#switch').data("id");
	var value = $('#adminSetDate').val();
	post("system/machine/editMachineDeadline",function (json, textStatus) {
		if(json!=null && json.success==true){
			$('#adminSet-modal').modal('hide');
			$('#mdeadline').text(defindEmpty(value));
			bootbox.alert("管理员设置修改命令已发送");
		}else{
			bootbox.alert("管理员设置失败:"+json.result);
		}
	},{machineId:id,date:new Date(value)});
}

function setRecoverSet(){
	var id = $('#switch').data("id");
	post("system/machine/recoverMachine",function (json, textStatus) {
		if(json!=null && json.success==true){
			bootbox.alert("恢复出厂设置修改命令已发送");
			setTimeout('getMachineById("'+id+'")',5000);
		}else{
			bootbox.alert("恢复出厂设置失败:"+json.result);
		}
	},{machineId:id});
}

function setTimeInterval(){
	var data={machineId:$('#switch').data("id")};
	
	var timeInterval1_1 = $('#workTimeInterval1-1').val();
	var timeInterval1_2 = $('#workTimeInterval1-2').val();
	if(!isEmpty(timeInterval1_1) && !isEmpty(timeInterval1_2)){
		data['machineWorkTimeInterval1']=timeInterval1_1+'-'+timeInterval1_2;
	}
	
	var timeInterval2_1 = $('#workTimeInterval2-1').val();
	var timeInterval2_2 = $('#workTimeInterval2-2').val();
	if(!isEmpty(timeInterval2_1) && !isEmpty(timeInterval2_2)){
		data['machineWorkTimeInterval2']=timeInterval2_1+'-'+timeInterval2_2;
	}
	
	var timeInterval3_1 = $('#workTimeInterval3-1').val();
	var timeInterval3_2 = $('#workTimeInterval3-2').val();
	if(!isEmpty(timeInterval3_1) && !isEmpty(timeInterval3_2)){
		data['machineWorkTimeInterval3']=timeInterval3_1+'-'+timeInterval3_2;
	}
	
	var timeInterval4_1 = $('#workTimeInterval4-1').val();
	var timeInterval4_2 = $('#workTimeInterval4-2').val();
	if(!isEmpty(timeInterval4_1) && !isEmpty(timeInterval4_2)){
		data['machineWorkTimeInterval4']=timeInterval4_1+'-'+timeInterval4_2;
	}
	
	var timeInterval5_1 = $('#workTimeInterval5-1').val();
	var timeInterval5_2 = $('#workTimeInterval5-2').val();
	if(!isEmpty(timeInterval5_1) && !isEmpty(timeInterval5_2)){
		data['machineWorkTimeInterval5']=timeInterval5_1+'-'+timeInterval5_2;
	}
	
	var timeInterval6_1 = $('#workTimeInterval6-1').val();
	var timeInterval6_2 = $('#workTimeInterval6-2').val();
	if(!isEmpty(timeInterval6_1) && !isEmpty(timeInterval6_2)){
		data['machineWorkTimeInterval6']=timeInterval6_1+'-'+timeInterval6_2;
	}
	
	var timeInterval7_1 = $('#workTimeInterval7-1').val();
	var timeInterval7_2 = $('#workTimeInterval7-2').val();
	if(!isEmpty(timeInterval7_1) && !isEmpty(timeInterval7_2)){
		data['machineWorkTimeInterval7']=timeInterval7_1+'-'+timeInterval7_2;
	}
	
	var timeInterval8_1 = $('#workTimeInterval8-1').val();
	var timeInterval8_2 = $('#workTimeInterval8-2').val();
	if(!isEmpty(timeInterval8_1) && !isEmpty(timeInterval8_2)){
		data['machineWorkTimeInterval8']=timeInterval8_1+'-'+timeInterval8_2;
	}
	
	postJson("system/machine/editMachineTimeInterval",function (json, textStatus) {
		if(json!=null && json.success==true){
			$('#timeInterval-modal').modal('hide');
			$('#mworktimeinterval1').text(defindEmpty(data.machineWorkTimeInterval1,'--'));
			$('#mworktimeinterval2').text(defindEmpty(data.machineWorkTimeInterval2,'--'));
			$('#mworktimeinterval3').text(defindEmpty(data.machineWorkTimeInterval3,'--'));
			$('#mworktimeinterval4').text(defindEmpty(data.machineWorkTimeInterval4,'--'));
			$('#mworktimeinterval5').text(defindEmpty(data.machineWorkTimeInterval5,'--'));
			$('#mworktimeinterval6').text(defindEmpty(data.machineWorkTimeInterval6,'--'));
			$('#mworktimeinterval7').text(defindEmpty(data.machineWorkTimeInterval7,'--'));
			$('#mworktimeinterval8').text(defindEmpty(data.machineWorkTimeInterval8,'--'));
			bootbox.alert("控制时间段设置修改命令已发送");
		}else{
			bootbox.alert("控制时间段设置失败:"+json.result);
		}
	},data);
}

function setTempTimeout(){
	var data={machineId:$('#switch').data("id")};
	
	var turnOffTemp = $('#turnOffTemp').val();
	if(!isEmpty(turnOffTemp)){
		data['machineTurnOffFireTemp']=turnOffTemp;
	}
	var turnOnTemp = $('#turnOnTemp').val();
	if(!isEmpty(turnOnTemp)){
		data['machineTurnOnFireTemp']=turnOnTemp;
	}
	var smokeTemp = $('#smokeTemp').val();
	if(!isEmpty(smokeTemp)){
		data['machineTurnSmokeTemp']=smokeTemp;
	}
	var turnOnPumpTemp = $('#turnOnPumpTemp').val();
	if(!isEmpty(turnOnPumpTemp)){
		data['machineTurnOnPumpTemp']=turnOnPumpTemp;
	}
	var turnOffPumpTemp = $('#turnOffPumpTemp').val();
	if(!isEmpty(turnOffPumpTemp)){
		data['machineTurnOffPumpTemp']=turnOffPumpTemp;
	}
	var turnOnTimeout = $('#turnOnTimeout').val();
	if(!isEmpty(turnOnTimeout)){
		data['machineTurnOnTimeout']=turnOnTimeout;
	}
	var preFeedTimeout = $('#preFeedTimeout').val();
	if(!isEmpty(preFeedTimeout)){
		data['machineTurnOnPreFeedTimeout']=preFeedTimeout;
	}
	var turnoffBlowerTimeout = $('#turnoffBlowerTimeout').val();
	if(!isEmpty(turnoffBlowerTimeout)){
		data['machineTurnOffBlowerTimeout']=turnoffBlowerTimeout;
	}
	var turnoffWindTimeout = $('#turnoffWindTimeout').val();
	if(!isEmpty(turnoffWindTimeout)){
		data['machineTurnOffWindTimeout']=turnoffWindTimeout;
	}
	
	postJson("system/machine/editMachineTempTimeout",function (json, textStatus) {
		if(json!=null && json.success==true){
			$('#tempTimeout-modal').modal('hide');
			$('#mturnofffiretemp').text(defindEmpty(turnOffTemp,'--'));
			$('#mturnonfiretemp').text(defindEmpty(turnOnTemp,'--'));
			$('#mturnsmoketemp').text(defindEmpty(smokeTemp,'--'));
			$('#mturnonpumptemp').text(defindEmpty(turnOnPumpTemp,'--'));
			$('#mturnoffpumptemp').text(defindEmpty(turnOffPumpTemp,'--'));
			$('#mturnontimeout').text(defindEmpty(turnOnTimeout,'--'));
			$('#mturnonprefeedtimeout').text(defindEmpty(preFeedTimeout,'--'));
			$('#mturnoffblowertimeout').text(defindEmpty(turnoffBlowerTimeout,'--'));
			$('#mturnoffwindtimeout').text(defindEmpty(turnoffWindTimeout,'--'));
			bootbox.alert("温度与时间参数设置成功");
		}else{
			bootbox.alert("温度与时间参数设置失败:"+json.result);
		}
	},data);
}

function nameFormatter(value, row, index){
	var icon = index % 2 === 0 ? 'glyphicon-star' : 'glyphicon-star-empty';
	return '<i class="glyphicon ' + icon + '"></i> ' + defindEmpty(value,'--');
}

function onlineStatusFormatter(value, row, index){
	if(value==0){
		return '离线';
	}else if(value==1){
		return '在线';
	}else{
		return value;
	}
}

function runStatusFormatter(value, row, index){
	if(value==0){
		return '关机';
	}else if(value==1){
		return '开机';
	}else{
		return value;
	}
}

function runingStatusFormatter(value, row, index){
	if(value==0){
		return '预进料';
	}else if(value==1){
		return '点火中';
	}else if(value==2){
		return '加热中';
	}else if(value==3){
		return '关火中';
	}else if(value==4){
		return '等待开机';
	}else{
		return value;
	}
}

function operateFormatter(value, row, index) {
    return [
        '<a class="detail" href="javascript:void(0);" title="详情">',
            '<i class="glyphicon glyphicon-edit"></i>详情',
        '</a>&nbsp;&nbsp;',
        '<a class="edit" href="javascript:void(0);" title="编辑">',
	        '<i class="glyphicon glyphicon-edit"></i>编辑',
	    '</a>&nbsp;&nbsp;',
        '<a class="remove" href="javascript:void(0);" title="删除">',
            '<i class="glyphicon glyphicon-remove"></i>删除',
        '</a>'
    ].join('');
}

window.operateEvents = {
	'click .detail': function (e, value, row, index) {
		getMachineById(row.machineId);
	},
	'click .edit': function (e, value, row, index) {
		post("system/machine/findMachineById",function (json, textStatus) {
			if(json!=null && json.success==true && json.result!=null){
				var machine = json.result;
				$('#machineId').val(machine.machineId);
				$("#machineNum").val(machine.machineNum);
				$("#machineNum").prop('readonly','readonly');
				$("#machineAname").val(machine.machineAname);
				
				getCustomer('customerId',machine.customerId,true);
				getCustomer('customerId1',machine.customerId1,true);
				getCustomer('customerId2',machine.customerId2,true);
				getCustomer('customerId3',machine.customerId3,true);
				
				$(".info-label").html("");
				$("#machineinfo-modal").modal("show");
			}else{
				bootbox.alert("获取用户信息失败");
			}
		},{machineId:row.machineId});
	},
	'click .remove': function (e, value, row, index) {
		bootbox.confirm("您确定要将设备删除吗？", function(result){
			if(result){
				del("system/machine/delMachines/"+row.machineId,function (json, textStatus) {
					if(json.success){
						bootbox.alert("设备删除成功");
						$('#machine-table').bootstrapTable("refresh",{silent: true});
					}else{
						bootbox.alert("设备删除失败");
					}
				});
			}
		});
	}
};

function getMachineById(id){
	post("system/machine/findMachineById",function (json, textStatus) {
		if(json!=null && json.success==true && json.result!=null){
			var machine = json.result;
			$('#mnum').text(machine.machineNum);
			$('#maname').text(defindEmpty(machine.machineAname,'--'));
			$('#mcname').text(defindEmpty(machine.machineCname,'--'));
			
			getCustomer('cid', machine.customerId);
			getCustomer('cid1', machine.customerId1);
			getCustomer('cid2', machine.customerId2);
			getCustomer('cid3', machine.customerId3);

			$('#switch').data("id",machine.machineId);
			$('#reflush').data("id",machine.machineId);
			$('#reflush').data("online",machine.machineOnlineStatus);
			var monlinestatus;
			if(machine.machineOnlineStatus==0){
				monlinestatus = '离线';
				$('#switch').hide();
			}else if(machine.machineOnlineStatus==1){
				monlinestatus = '在线';
				$('#switch').show();
			}else{
				monlinestatus = '--';
				$('#switch').hide();
			}
			$('#monlinestatus').text(monlinestatus);
			
			var mrunstatus;
			if(machine.machineRunStatus==0){
				mrunstatus = '关机';
				$('#switch').addClass('btn-success');
				$('#switch').removeClass('btn-danger');
				$('#switch').text('开机');
				$('#switch').data('value','true');
			}else if(machine.machineRunStatus==1){
				mrunstatus = '开机';
				$('#switch').removeClass('btn-success');
				$('#switch').addClass('btn-danger');
				$('#switch').text('关机');
				$('#switch').data('value','false');
			}else{
				mrunstatus = '--';
				$('#switch').addClass('btn-success');
				$('#switch').removeClass('btn-danger');
				$('#switch').text('开机');
				$('#switch').data('value','true');
			}
			$('#mrunstatus').text(mrunstatus);
			
			var mruningstatus;
			if(machine.machineRuningStatus==0){
				mruningstatus = '预进料';
			}else if(machine.machineRuningStatus==1){
				mruningstatus = '点火中';
			}else if(machine.machineRuningStatus==2){
				mruningstatus = '加热中';
			}else if(machine.machineRuningStatus==3){
				mruningstatus = '关火中';
			}else{
				mruningstatus = '--';
			}
			$('#mruningstatus').text(mruningstatus);
			
			var mbinstatus;
			if(machine.machineBinStatus==0){
				mbinstatus = '关闭';
			}else if(machine.machineBinStatus==1){
				mbinstatus = '开启';
			}else{
				mbinstatus = '--';
			}
			$('#mbinstatus').text(mbinstatus);
			
			var mstovestatus;
			if(machine.machineStoveStatus==0){
				mstovestatus = '关闭';
			}else if(machine.machineStoveStatus==1){
				mstovestatus = '开启';
			}else{
				mstovestatus = '--';
			}
			$('#mstovestatus').text(mstovestatus);
			
			var mblowerstatus;
			if(machine.machineBlowerStatus==0){
				mblowerstatus = '关闭';
			}else if(machine.machineBlowerStatus==1){
				mblowerstatus = '开启';
			}else{
				mblowerstatus = '--';
			}
			$('#mblowerstatus').text(mblowerstatus);
			
			var mwindstatus;
			if(machine.machineWindStatus==0){
				mwindstatus = '关闭';
			}else if(machine.machineWindStatus==1){
				mwindstatus = '开启';
			}else{
				mwindstatus = '--';
			}
			$('#mwindstatus').text(mwindstatus);
			
			var mpumpstatus;
			if(machine.machinePumpStatus==0){
				mpumpstatus = '关闭';
			}else if(machine.machinePumpStatus==1){
				mpumpstatus = '开启';
			}else{
				mpumpstatus = '--';
			}
			$('#mpumpstatus').text(mpumpstatus);
			
			var mfirestatus;
			if(machine.machineFireStatus==0){
				mfirestatus = '关闭';
			}else if(machine.machineFireStatus==1){
				mfirestatus = '开启';
			}else{
				mfirestatus = '--';
			}
			$('#mfirestatus').text(mfirestatus);
			
			var mfiresuccess;
			if(machine.machineFireSuccess===true){
				mfiresuccess = '点火成功';
			}else if(machine.machineFireSuccess===false){
				mfiresuccess = '点火失败';
			}else{
				mfiresuccess = '--';
			}
			$('#mfiresuccess').text(mfiresuccess);
			
			var moutofwater;
			if(machine.machineOutofWater==true){
				moutofwater = '缺水';
			}else if(machine.machineOutofWater==false){
				moutofwater = '不缺水';
			}else{
				moutofwater = '--';
			}
			$('#moutofwater').text(moutofwater);
			
			$('#mwatertemp').text(defindEmpty(machine.machineWaterTemp,'--'));
			$('#msmoketemp').text(defindEmpty(machine.machineSmokeTemp,'--'));
			$('#minletwatertemp').text(defindEmpty(machine.machineInletWaterTemp,'--'));
			
			$('#mbinstall').text(defindEmpty(machine.machineBinStall,'--'));
			$('#binStall').val(defindEmpty(machine.machineBinStall,0));
			$('#mturnofffiretemp').text(defindEmpty(machine.machineTurnOffFireTemp,'--'));
			$('#turnOffTemp').val(defindEmpty(machine.machineTurnOffFireTemp));
			$('#mturnonfiretemp').text(defindEmpty(machine.machineTurnOnFireTemp,'--'));
			$('#turnOnTemp').val(defindEmpty(machine.machineTurnOnFireTemp));
			$('#mturnsmoketemp').text(defindEmpty(machine.machineTurnSmokeTemp,'--'));
			$('#smokeTemp').val(defindEmpty(machine.machineTurnSmokeTemp));
			$('#mturnonpumptemp').text(defindEmpty(machine.machineTurnOnPumpTemp,'--'));
			$('#turnOnPumpTemp').val(defindEmpty(machine.machineTurnOnPumpTemp));
			$('#mturnoffpumptemp').text(defindEmpty(machine.machineTurnOffPumpTemp,'--'));
			$('#turnOffPumpTemp').val(defindEmpty(machine.machineTurnOffPumpTemp));
			$('#mturnontimeout').text(defindEmpty(machine.machineTurnOnTimeout,'--'));
			$('#turnOnTimeout').val(defindEmpty(machine.machineTurnOnTimeout));
			$('#mturnonprefeedtimeout').text(defindEmpty(machine.machineTurnOnPreFeedTimeout,'--'));
			$('#preFeedTimeout').val(defindEmpty(machine.machineTurnOnPreFeedTimeout));
			$('#mturnoffblowertimeout').text(defindEmpty(machine.machineTurnOffBlowerTimeout,'--'));
			$('#turnoffBlowerTimeout').val(defindEmpty(machine.machineTurnOffBlowerTimeout));
			$('#mturnoffwindtimeout').text(defindEmpty(machine.machineTurnOffWindTimeout,'--'));
			$('#turnoffWindTimeout').val(defindEmpty(machine.machineTurnOffWindTimeout));
			$('#mworktimeinterval1').text(defindEmpty(machine.machineWorkTimeInterval1,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval1)){
				var tts = machine.machineWorkTimeInterval1.split('-');
				$('#workTimeInterval1-1').val(tts[0]);
				$('#workTimeInterval1-2').val(tts[1]);
			}
			$('#mworktimeinterval2').text(defindEmpty(machine.machineWorkTimeInterval2,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval2)){
				var tts = machine.machineWorkTimeInterval2.split('-');
				$('#workTimeInterval2-1').val(tts[0]);
				$('#workTimeInterval2-2').val(tts[1]);
			}
			$('#mworktimeinterval3').text(defindEmpty(machine.machineWorkTimeInterval3,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval3)){
				var tts = machine.machineWorkTimeInterval3.split('-');
				$('#workTimeInterval3-1').val(tts[0]);
				$('#workTimeInterval3-2').val(tts[1]);
			}
			$('#mworktimeinterval4').text(defindEmpty(machine.machineWorkTimeInterval4,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval4)){
				var tts = machine.machineWorkTimeInterval4.split('-');
				$('#workTimeInterval4-1').val(tts[0]);
				$('#workTimeInterval4-2').val(tts[1]);
			}
			$('#mworktimeinterval5').text(defindEmpty(machine.machineWorkTimeInterval5,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval5)){
				var tts = machine.machineWorkTimeInterval5.split('-');
				$('#workTimeInterval5-1').val(tts[0]);
				$('#workTimeInterval5-2').val(tts[1]);
			}
			$('#mworktimeinterval6').text(defindEmpty(machine.machineWorkTimeInterval6,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval6)){
				var tts = machine.machineWorkTimeInterval6.split('-');
				$('#workTimeInterval6-1').val(tts[0]);
				$('#workTimeInterval6-2').val(tts[1]);
			}
			$('#mworktimeinterval7').text(defindEmpty(machine.machineWorkTimeInterval7,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval7)){
				var tts = machine.machineWorkTimeInterval7.split('-');
				$('#workTimeInterval7-1').val(tts[0]);
				$('#workTimeInterval7-2').val(tts[1]);
			}
			$('#mworktimeinterval8').text(defindEmpty(machine.machineWorkTimeInterval8,'--'));
			if(!isEmpty(machine.machineWorkTimeInterval8)){
				var tts = machine.machineWorkTimeInterval8.split('-');
				$('#workTimeInterval8-1').val(tts[0]);
				$('#workTimeInterval8-2').val(tts[1]);
			}
			$('#mdeadline').text(defindEmpty(machine.machineAdminDeadline,'--'));
			$('#adminSetDate').val(defindEmpty(machine.machineAdminDeadline))
			
			$('#maddress').text(defindEmpty(machine.machineAddress,'--'));
			$('#mcreatetime').text(defindEmpty(machine.createTime,'--'));
			$('#mmodifytime').text(defindEmpty(machine.modifyTime,'--'));
			$('#mhearttime').text(defindEmpty(machine.heartTime,'--'));
			
			$('#machineDetail').removeClass('hidden');
		}else{
			bootbox.alert("获取用户信息失败");
		}
	},{machineId:id});
}

function getCustomer(id, customerId, isEdit){
	if(isEmpty(customerId)){
		$('#'+id).text('--');
	}else{
		post("system/customer/findCustomerById",function (json, textStatus) {
			if(json!=null && json.success==true && json.result!=null){
				var data = json.result;
				if(isEdit==true){
					$("#"+id).val(data.customerPhone);
				}else{
					$('#'+id).text(data.customerPhone);
				}
			}else{
				if(isEdit==true){
					$("#"+id).val('');
				}else{
					$('#'+id).text('--');
				}
			}
		},{customerId:customerId});
	}
}